#include "ASScrMain.h"
#include "ASScreenMgr.h"
#include "..\app\asappimginfo.h"
#include "..\control\ASAdvbar.h"
#include "ASScrCategory.h"
#include "ASScrList.h"
#include "ASScrDetail.h"
#include "mrc_advbar.h"
#include "ASWaitingScreen.h"
#include "res_str.h"
#include "ASScreenShare.h"
#include "..\app\ASAppConst.h"
#include "..\app\ASLogic.h"
#include "ASConfirmScreen.h"
#include "ASScrProcess.h"
#include "ASScrSetting.h"
#include "..\tool\ASFreePay.h"

static as_screen_setting* s_scr_setting;

static void as_scr_setting_add_adv(as_screen* screen_p)
{
    as_rect area;
    area.x = 0;
    area.y = 0;
    area.w = as_get_lcd_width();
    area.h = as_app_get_advbar_height();
    as_screen_add_advbar(screen_p, area);
}

static void as_scr_setting_music_evt(void)
{
    as_global_config* config_p = as_get_global_config();

    if (config_p->music_switch == ON)
    {
        config_p->music_switch = OFF;
        s_scr_setting->img_music_off->base.set_hidden((as_control*)s_scr_setting->img_music_off, ASFALSE);
        s_scr_setting->img_music_on->base.set_hidden((as_control*)s_scr_setting->img_music_on, ASTRUE);
    }
    else
    {
        config_p->music_switch = ON;
        s_scr_setting->img_music_off->base.set_hidden((as_control*)s_scr_setting->img_music_off, ASTRUE);
        s_scr_setting->img_music_on->base.set_hidden((as_control*)s_scr_setting->img_music_on, ASFALSE);
    }

    s_scr_setting->base.paint_hdlr((as_screen*)s_scr_setting);
    s_scr_setting->base.flush_hdlr((as_screen*)s_scr_setting);
}


static void as_scr_setting_rsk_evt(void)
{
    as_show_exit();
}

static void as_scr_setting_lsk_evt(void)
{
	as_scr_setting_music_evt();
}

static void as_scr_setting_create_softkey(as_screen* screen_p)
{
    as_screen_create_softkey_bar(screen_p, TEXT_SETTING, TEXT_EXIT, as_scr_setting_lsk_evt, as_scr_setting_rsk_evt);
}

static void as_scr_ascii_to_ucs2(const ASSTR src1, const ASSTR src2, ASWSTR dst1, ASWSTR dst2, ASINT dst1len, ASINT dst2len)
{
    as_memset(dst1, 0, dst1len);
    as_memset(dst2, 0, dst2len);
    as_ascii_to_ucs2(dst1, src1);
    as_ascii_to_ucs2(dst2, src2);
}

static void as_scr_setting_entry_main(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->delete_and_pop_screen();
    as_scr_main_show();
}

static void as_scr_setting_entry_process(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->delete_and_pop_screen();
    as_scr_process_show();
}

static void as_scr_setting_tab_evt(ASINT idx)
{
    switch(idx)
    {
        case 0:
            as_scr_setting_entry_main();
            break;

        case 1:
            as_scr_setting_entry_process();
            break;

        case 2:
            break;
    }
}


static void as_scr_setting_add_head(as_screen* screen_p)
{
    as_rect area;
    ASINT w, h, strh;
    ASUINT param[6];
    ASWCHAR tmp[30], tmp2[30];
    as_tablebar* tab_p;
    as_get_internal_img_res_size(IMG_TAB_VIEW, &w, &h);
    strh = as_get_font_height();
    h = h > strh ? h : strh;
    s_scr_setting->head_height = h + as_app_get_advbar_height();
    area.w = as_get_lcd_width();
    area.h = (ASINT16)h;
    area.x = 0;
    area.y = as_app_get_advbar_height();
    param[0] = 3;
    param[1] = AS_TABLEBAR_IMAGE_TYPE;
    param[2] = AS_TABLEBAR_SHOW_HORIZONTAL;
    tab_p = (as_tablebar*)screen_p->add_ctrl(screen_p, AS_CTRL_TABLEBAR, area, 3, (void**)param);
    as_scr_ascii_to_ucs2(IMG_TAB_VIEW, IMG_TAB_VIEW_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    as_scr_ascii_to_ucs2(IMG_TAB_PROCESS, IMG_TAB_PROCESS_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    as_scr_ascii_to_ucs2(IMG_TAB_MORE, IMG_TAB_MORE_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    tab_p->set_bg_color((as_control*)tab_p, 0x0);
    tab_p->change_focus_idx((as_control*)tab_p, 2);
    tab_p->set_cb((as_control*)tab_p, as_scr_setting_tab_evt);
    s_scr_setting->tab = tab_p;
}

static void as_scr_setting_to_ucs2(const ASSTR src1, const ASSTR src2, ASWSTR dst1, ASWSTR dst2, ASINT dst1len, ASINT dst2len)
{
    as_memset(dst1, 0, dst1len);
    as_memset(dst2, 0, dst2len);
    as_ascii_to_ucs2(dst1, src1);
    as_ascii_to_ucs2(dst2, src2);
}

static as_button* as_scr_setting_add_button(as_screen* screen_p, as_rect area, ASINT type, as_image** img_cn, as_image**img_en)
{
    as_rect area2;
    ASSTR str_cn, str_en;
    as_global_config* config_p;
    as_button* button;
    ASWCHAR dst1[30], dst2[30];
    ASUINT param[5];
    config_p = as_get_global_config();
    as_scr_setting_to_ucs2(IMG_MUSIC, IMG_MUSIC_D, dst1, dst2, 30, 30);
    param[0] = AS_IMAGE_BUTTON;
    param[1] = (ASUINT)dst1;
    param[2] = (ASUINT)dst2;
    param[3] = (ASUINT)ASNULL;
    param[4] = AS_IMAGE_FROM_RES;
    button = (as_button*)screen_p->add_ctrl(screen_p, AS_CTRL_BUTTON, area, 5, (void**)param);
    area2.x = area.x;
    area2.y = area.y + area.h;
    area2.w = IMG_MUSIC_OFF_W;
    area2.h = IMG_MUSIC_OFF_H;
    param[0] = AS_IMAGE_FROM_RES;
    str_en = IMG_MUSIC_ON;
    str_cn = IMG_MUSIC_OFF;
    param[1] = (ASUINT)as_ucs2_from_ascii(str_en);
    param[2] = ASFALSE;
    *img_en = (as_image*)screen_p->add_ctrl(screen_p, AS_CTRL_IMAGE, area2, 3, (void**)param);
    param[2] = (ASUINT)as_ucs2_from_ascii(str_cn);
    *img_cn = (as_image*)screen_p->add_ctrl(screen_p, AS_CTRL_IMAGE, area2, 3, (void**)param);
    return button;
}

static void as_scr_setting_add_button_set(as_screen* screen_p)
{
    as_button* button_p;
    as_rect area;
    area.x = 10;
    area.y = s_scr_setting->head_height + 20;
    area.w = IMG_MUSIC_W;
    area.h = IMG_MUSIC_H;
    button_p = as_scr_setting_add_button(screen_p, area, 0, &s_scr_setting->img_music_on, &s_scr_setting->img_music_off);
    button_p->set_cb_by_status((as_control*)button_p, AS_BUTTON_UP, as_scr_setting_music_evt);
}

void as_scr_setting_init_config(void)
{
    as_global_config* config_p = as_get_global_config();

    if (config_p->music_switch == ON)
    {
        if (s_scr_setting->img_music_on != ASNULL || s_scr_setting->img_music_off != ASNULL)
        {
            s_scr_setting->img_music_on->base.set_hidden((as_control*)s_scr_setting->img_music_on, ASFALSE);
            s_scr_setting->img_music_off->base.set_hidden((as_control*)s_scr_setting->img_music_off, ASTRUE);
        }
    }
    else
    {
        if (s_scr_setting->img_music_on != ASNULL || s_scr_setting->img_music_off != ASNULL)
        {
            s_scr_setting->img_music_on->base.set_hidden((as_control*)s_scr_setting->img_music_on, ASTRUE);
            s_scr_setting->img_music_off->base.set_hidden((as_control*)s_scr_setting->img_music_off, ASFALSE);
        }
    }
}
void as_scr_setting_init(as_screen* screen_p)
{
    s_scr_setting = (as_screen_setting*)screen_p;
    screen_p->set_bg_color(screen_p, 0xAE1C1C);
    as_scr_setting_add_adv(screen_p);
    as_scr_setting_add_head(screen_p);
    as_scr_setting_create_softkey(screen_p);
    as_scr_setting_add_button_set(screen_p);
    as_scr_setting_init_config();
}

void as_scr_setting_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_scr_setting_show(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    as_screen* scr_p;
    as_ret ret;
    ret = scr_mgr->new_and_push_screen(AS_SCREEN_SETTING);
    scr_p = scr_mgr->get_cur_screen();

    if (scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}